import Vue from 'vue';
import Vuex from 'vuex';
import { getAdminInfo } from '@/api/admin';
Vue.use(Vuex)

//actions 方法名小写
const actions = {
    updateWebSocket(context,socket){
        context.commit("UPDATEWEBSOCKET",socket);
    },
    resetAdminInfo(context){
        context.commit("RESETADMININFO");
    },
    getAdminInfo(context){
        return getAdminInfo().then(res => {
            if(res.code == 200){
                context.commit("SETADMINID",res.data.adminId)
                context.commit("SETACCOUNT",res.data.account)
                context.commit("SETROLENAME",res.data.roleDO.roleName)
                return true;
            }else{
                return false;
            }
        })
    },

    setAdminId(context,value){
        context.commit("SETADMINID",value);
    },
    setAccount(context,value){
        context.commit("SETACCOUNT",value);
    },
    setRoleName(context,value){
        context.commit("SETROLENAME",value);
    }
}

//mutations 方法名大写
const mutations = {
    UPDATEWEBSOCKET(state,socket){
        state.webSocket=socket
    },
    SETADMINID(state, value){
        state.adminId = value
    },
    SETACCOUNT(state, value){
        state.account = value
    },
    SETROLENAME(state, value){
        state.roleName = value
    },
    RESETADMININFO(state) {
    state.adminId = null,
    state.account = null;
    state.roleName = null;
  }
}

const state = {
   adminId: null,
   account: null,
   roleName: null,
}

//创建Vuex的实例
const store = new Vuex.Store({
    actions,
    mutations,
    state,
})

//暴露
export default store;
